package org.zjx.swing.db;

import org.zjx.swing.bean.Font;

import java.util.List;

public class FontDB {

    private static boolean isTableCreated = false;  // 标志变量，确保表只创建一次

    private SQLiteHelper dbHelper;

    public FontDB() {
        dbHelper = SQLiteHelper.getSingleton();
        // 仅在第一次实例化时创建表
        if (!isTableCreated) {
            createTable();
            isTableCreated = true;  // 标志位更新，后续不会再创建表
        }
    }

    // 创建表
    public void createTable() {
        String createTable = "CREATE TABLE IF NOT EXISTS font (\n"
                + "    id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
                + "    type INTEGER NOT NULL,\n"
                + "    name TEXT NOT NULL,\n"
                + "    block TEXT NOT NULL,\n"
                + "    wide INTEGER NOT NULL,\n"
                + "    high INTEGER NOT NULL\n"
                + ");";
        dbHelper.createTable(createTable);
    }

    // SQLite的插入，如果name已经存在，则更新其他字段
    public void insert(Font font) {
        String sql = "INSERT INTO font( type, name, block, wide, high) VALUES( ?, ?, ?, ?, ?)";
        Object[] params = {  font.getType(), font.getName(), font.getBlock(), font.getWide(), font.getHigh() };
        dbHelper.executeUpdate(sql, params);
    }

    public void deleteById(int id) {
        String sql = "DELETE FROM font WHERE id = ?";
        Object[] params = { id };
        dbHelper.executeUpdate(sql, params);
    }

    public void deleteByName(String name) {
        String sql = "DELETE FROM font WHERE name = ?";
        Object[] params = { name };
        dbHelper.executeUpdate(sql, params);
    }

    public Font findById(int id) {
        String sql = "SELECT * FROM font WHERE id = ?";
        Object[] params = { id };
        List<Font> fonts = dbHelper.executeQuery(sql, params, Font.class);
        if (fonts.isEmpty()) return null;
        return fonts.get(0);
    }

    public Font findByName(String name) {
        String sql = "SELECT * FROM font WHERE name = ?";
        Object[] params = { name };
        List<Font> fonts = dbHelper.executeQuery(sql, params, Font.class);
        if (fonts.isEmpty()) return null;
        return fonts.get(0);
    }

    public Font findByName(String name,int type) {
        String sql = "SELECT * FROM font WHERE name = ? AND type = ?";
        Object[] params = { name, type };
        List<Font> fonts = dbHelper.executeQuery(sql, params, Font.class);
        if (fonts.isEmpty()) return null;
        return fonts.get(0);
    }

    public Font findByBlock(String block,int type) {
        String sql = "SELECT * FROM font WHERE block = ? AND type = ?";
        Object[] params = { block , type };
        List<Font> fonts = dbHelper.executeQuery(sql, params, Font.class);
        if (fonts.isEmpty()) return null;
        return fonts.get(0);
    }

    public List<Font> findAll() {
        String sql = "SELECT * FROM font";
        Object[] params = {};
        return dbHelper.executeQuery(sql, params, Font.class);
    }

    // 根据id修改font的其他信息
    public void updateFontById(int id, Font newFont) {
        String sql = "UPDATE font SET type = ?, name = ?, block = ?, wide = ?, high = ? WHERE id = ?";
        Object[] params = { newFont.getType(), newFont.getName(), newFont.getBlock(), newFont.getWide(), newFont.getHigh(), id };
        dbHelper.executeUpdate(sql, params);
    }

    public void clearData() {
        dbHelper.clearData("font");
    }

    public void dropTable() {
        dbHelper.dropTable("font");
    }
}
